﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Web;
using DotNet.Highcharts.Enums;
using DotNet.Highcharts.Helpers;
using DotNet.Highcharts.Options;

namespace WebApplication12
{
    /// <summary>
    /// Summary description for servis
    /// </summary>
    public class servis : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            Exception izjema = null;
            Obcina rezultat = null;
            try
            {
                using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["StatistikaConnectionString"].ConnectionString))
                {
                    using (SqlCommand com = new SqlCommand(
                        "SELECT population, mens, womens, natural_annual_growth, cars " +
                        "FROM Statistic_population " +
                        "INNER JOIN Statistic_cars ON Statistic_cars.fk_municipalityID = Statistic_population.fk_municipalityID " +
                        "WHERE Statistic_population.fk_municipalityID = @obcina", con))
                    {
                        com.Parameters.AddWithValue("@obcina", context.Request.QueryString["ObcinaID"]);

                        con.Open();
                        using (SqlDataReader sdr = com.ExecuteReader())
                        {
                            if (sdr.Read())
                            {
                                rezultat = new Obcina();
                                rezultat.StPrebivalcev = int.Parse(sdr["population"].ToString());
                                rezultat.StMoskih = int.Parse(sdr["mens"].ToString());
                                rezultat.StZensk = int.Parse(sdr["womens"].ToString());
                                rezultat.NaravniPrirastek = int.Parse(sdr["natural_annual_growth"].ToString());
                                rezultat.StAvtomobilov = int.Parse(sdr["cars"].ToString());

                                DotNet.Highcharts.Highcharts chart = new DotNet.Highcharts.Highcharts("chart2")
                                    .InitChart(new Chart { PlotBackgroundColor = null, PlotBorderWidth = null, PlotShadow = false })
                                    .SetTitle(new Title { Text = "" })
                                    .SetTooltip(new Tooltip { PointFormat = "{series.name}: <b>{point.percentage}%</b>", ValueDecimals = 2,  /*, percentageDecimals: 1*/ })
                                    .SetPlotOptions(new PlotOptions
                                    {
                                        Pie = new PlotOptionsPie
                                        {
                                            Size = new PercentageOrPixel(50, true),
                                            ShowInLegend = true,
                                            AllowPointSelect = true,
                                            Cursor = Cursors.Pointer,
                                            DataLabels = new PlotOptionsPieDataLabels
                                            {
                                                Enabled = false,
                                            }
                                        }
                                    }).SetSeries(new Series
                                    {
                                        Type = ChartTypes.Pie,
                                        Name = "Delež",
                                        Data = new Data(new object[]
                                        {
                                            new object[] { "Moški", rezultat.StMoskih },
                                            new object[] { "Ženske", rezultat.StZensk }
                                        })
                                    });

                                rezultat.Graf = chart.ToHtmlString();
                            }
                        }
                    }
                }
            }
            catch (Exception exc) { izjema = exc; }

            context.Response.ContentType = "application/json";

            if (izjema == null)
            {
                var javaScriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                string jsonString = javaScriptSerializer.Serialize(rezultat);
                context.Response.Write(jsonString);
            }
            else
                context.Response.Write(null);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public class Obcina
        {
            public int StPrebivalcev { get; set; }
            public int StMoskih { get; set; }
            public int StZensk { get; set; }
            public int NaravniPrirastek { get; set; }
            public int StAvtomobilov { get; set; }

            public string Graf { get; set; }
        }
    }
}